package com.example.parkingproject.controller;


import com.example.parkingproject.jopo.dto.AddAdminUserDto;
import com.example.parkingproject.jopo.dto.AdminLoginDto;
import com.example.parkingproject.jopo.dto.UpdateAdminUserDto;
import com.example.parkingproject.jopo.vo.AdminUserVo;
import com.example.parkingproject.result.JsonResult;
import com.example.parkingproject.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("login")
    public JsonResult<String> login(@RequestBody AdminLoginDto adminLoginDto) {
        String jwt = adminService.login(adminLoginDto);
        return JsonResult.ok(jwt);
    }

    /**
     * 添加新的管理员用户
     *
     * @param addAdminUserDto
     * @return
     */
    @RequestMapping("/AddAdminUser")
    @PreAuthorize("hasAnyAuthority('1')")
    public JsonResult AddAdminUser(@RequestBody AddAdminUserDto addAdminUserDto) {
        adminService.AddAdminUser(addAdminUserDto);
        return JsonResult.ok();
    }

    /**
     * 获取所有管理员用户列表
     *
     * @return
     */
    @RequestMapping("/AdminList")
    public JsonResult<List<AdminUserVo>> AdminList() {
        List<AdminUserVo> list = adminService.AdminList();
        log.debug("查询出的参数{}", list);
        return JsonResult.ok(list);
    }

    /**
     * 获取单个的管理员用户信息
     *
     * @param cardId
     * @return
     */
    @RequestMapping("/getAdminByCard")
    public JsonResult<AdminUserVo> getAdminByCard(String cardId) {
        AdminUserVo adminByCard = adminService.getAdminByCard(cardId);
        return JsonResult.ok(adminByCard);
    }


    /**
     * 管理员用户对个人信息进行更改
     *
     * @param updateAdminUserDto
     * @return
     */
    @RequestMapping("/updateAdminAll")
    @PreAuthorize("hasAnyAuthority('1')")
    public JsonResult updateAdminAll(@RequestBody UpdateAdminUserDto updateAdminUserDto) {
        adminService.updateAdminAll(updateAdminUserDto);
        return JsonResult.ok();
    }

    @RequestMapping("/deleteAdmin")
    @PreAuthorize("hasAnyAuthority('1')")
    public JsonResult deleteAdmin(String cardId) {
        log.debug("传入参数为{}", cardId);
        adminService.deleteAdmin(cardId);
        return JsonResult.ok();
    }
    @RequestMapping("/search")
    public JsonResult<List<AdminUserVo>> search(@RequestBody AddAdminUserDto addAdminUserDto){
        List<AdminUserVo> list = adminService.search(addAdminUserDto);
        log.debug("返回参数{}",list);
        return JsonResult.ok(list);
    }

    @RequestMapping("/enable")
    @PreAuthorize("hasAnyAuthority('1')")
    public JsonResult setEnable(Integer id) {
        log.debug("传入参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @RequestMapping("/disable")
    @PreAuthorize("hasAnyAuthority('1')")
    public JsonResult setDisable( Integer id) {
        log.debug("传入参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

}
